package com.leetcode.June;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 二叉树的中序遍历
 * @Author: Mr.Fxy
 * @CreateTime: 2024-07-09 17:53
 */
class TreeNodes {
    int val;
    TreeNodes left;
    TreeNodes right;

    TreeNodes() {
    }

    TreeNodes(int val) {
        this.val = val;
    }

    TreeNodes(int val, TreeNodes left, TreeNodes right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
public class centreTree {

    public static  List<Integer> inorderTraversal(TreeNodes root) {
        List<Integer> res = new ArrayList<Integer>();
        inorder(root, res);
        return res;

    }
    private static void inorder(TreeNodes root, List<Integer> res) {
        if (root == null) return;
        inorder(root.left, res);
        res.add(root.val);
        inorder(root.right, res);

    }

    public static void main(String[] args) {
        // 创建二叉树
        TreeNodes node3 = new TreeNodes(3);
        TreeNodes node2 = new TreeNodes(2, node3, null);
        TreeNodes node1 = new TreeNodes(1, null, node2);

        // 实例化CentreTree类并调用inorderTraversal方法

        List<Integer> result = inorderTraversal(node1);

        // 打印结果
        System.out.println(result);  // 输出: [1, 3, 2]
    }

}
